Page({
  data: {
    imgB64: '',
    img:'',
    ishow: false,
    imgData:'',
    ifCombine:false,
    ifBg:false,
    ifKoutu:true
  },
  refreshTap: function() {  
    // 重置图片路径或Base64编码  
    this.setData({  
      img: '',  
      imgData: '',  
    });  
    // 根据需要重置其他变量或调用其他函数来刷新页面  
    this.setData({  
      ishow: true, // 显示提示信息，提示用户重新选择图片  
      ifKoutu:true
    });  
  },  
  chooseimgTap: function () {
    this.setData({ishow: false});
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths[0];
        var imgB64 = wx.getFileSystemManager().readFileSync(tempFilePaths, "base64")
        that.setData({
          img: tempFilePaths,
          imgB64: imgB64
        });
      }
    })
  },
  chooseBg: function () {
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths[0];
        that.setData({
          img1: tempFilePaths,
          ifCombine:true,
          ifBg:false
        });
        wx.showModal({
          title: '提示',
          content: '背景上传成功是否合成新图?',
          success(res)
          {
            if(res.confirm)
            {
              that.conmbineTap(tempFilePaths)
            }
            else
            {
              return
            }
          }
        })
      }
    })
    
  },
  koutuTap: function (e) {
    const imgB64 = this.data.imgB64;
    if (!imgB64) {
      this.setData({ishow: true});
      return;
    } 
    this.getToken()
    this.getResult()
  },
  getToken:function() {
    let that = this
    wx.request({
      url: 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=fZYvk1lmfZSsGzRei8F0Kdjt&client_secret=e5QsOPcf2oRNmmho5RMEchKgCeIHUh7w',
      success(res) {
        if (res.data)
        {
            wx.setStorageSync('key1', res.data.access_token)
        }
      }
    })
  },
  getResult: function () {
    let that = this
    wx.showLoading({
      title: '处理中。。。',
    })
    wx.request({
      url: 'https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg',
      method: "post",
      data: {
        access_token: wx.getStorageSync('key1'),
        image: that.data.imgB64,
        type: 'foreground'
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        if (res.data.person_num) {
          var imgData = res.data.foreground
          that.setData({ 
            imgData: res.data.foreground,
             })
          wx.hideLoading()
          that.savePic(imgData)
        }
        else
        {
          wx.showToast({
            title: '处理失败，请重试',
          })
        }
      }
    })
  },
  savePic: function (imgSrc){
    let that = this
    var save = wx.getFileSystemManager()
    var number = new Date().getTime()
    var filePath = wx.env.USER_DATA_PATH +'/'+ number + '.png'
    save.writeFile({
      filePath: filePath,
      data: imgSrc,
      encoding: 'base64',
      success: res => {
            wx.showToast({
              title: '保存成功',
            })
            that.setData({
              ifBg: true,
              ifKoutu:false,
              filePath: filePath
               })
          },
      fail: function (err) {
        console.log(err)
      }
    })
  },
  conmbineTap: function (tempFile){
    let that = this
    wx.navigateTo({
      url: '../combine/combine?imgUrl=' + that.data.filePath + '&img1=' + tempFile
    })
  },
})
